package vn.hocweb.dao.dao_implement;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import vn.hocweb.dao.dao_interface.IMaTaiVeDAO;
import vn.hocweb.entity.MaTaiVe;

public class MaTaiVeDAO extends GenericDAO implements IMaTaiVeDAO{

	@Override
	public MaTaiVe findByID(String id) {
		Connection connection = getConnection();
		MaTaiVe result = null;
		String sql = "select * from mataive where mataive = ?";
		try {
			PreparedStatement pre = connection.prepareStatement(sql);
			pre.setString(1, id);
			ResultSet effected = pre.executeQuery();
			if (effected.next()){
				String maTaiVe = effected.getString("mataive");
				
				int soLanTaiVe = effected.getInt("solantaive");
				int gioiHanTaiVe = effected.getInt("gioihantaive");
				int thuTu = effected.getInt("thutu");
				boolean noiBat = effected.getBoolean("noibat");
				boolean hienThi = effected.getBoolean("hienthi");
				
				result = new MaTaiVe(thuTu, noiBat, hienThi, maTaiVe, 
						soLanTaiVe, gioiHanTaiVe);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		freeConnection(connection);
		return result;
	}

	@Override
	public List<MaTaiVe> findAll(int firstItem, int batchSize) {
		Connection connection = getConnection();
		List<MaTaiVe> result = new ArrayList<MaTaiVe>();
		String sql = "select * from mataive limit ?, ?";
		try {
			PreparedStatement pre = connection.prepareStatement(sql);
			pre.setLong(1, firstItem);
			pre.setLong(2, batchSize);
			ResultSet effected = pre.executeQuery();
			while (effected.next()){
				String maTaiVe = effected.getString("mataive");
				
				int soLanTaiVe = effected.getInt("solantaive");
				int gioiHanTaiVe = effected.getInt("gioihantaive");
				int thuTu = effected.getInt("thutu");
				boolean noiBat = effected.getBoolean("noibat");
				boolean hienThi = effected.getBoolean("hienthi");
				
				MaTaiVe row = new MaTaiVe(thuTu, noiBat, hienThi, maTaiVe,
						soLanTaiVe, gioiHanTaiVe);
				result.add(row);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		freeConnection(connection);
		return result;
	}

	@Override
	public int size() {
		Connection connection = getConnection();
		int result = 0;
		String sql = "select count(*) from mataive";
		try {
			PreparedStatement pre = connection.prepareStatement(sql);
			ResultSet effected = pre.executeQuery();
			if(effected.next()){
				result = effected.getInt(0);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		freeConnection(connection);
		return result;
	}

	@Override
	public boolean save(MaTaiVe entity) {
		Connection connection = getConnection();
		boolean result = false;
		String sql = "insert into mataive (mataive, thutu, noibat, hienthi, solantaive, gioihantaive) " +
				"values (?, ?, ?, ?, ?, ?)";
		try {
			PreparedStatement pre = connection.prepareStatement(sql);
			pre.setString(1, entity.getMaTaiVe());
			pre.setInt(2, entity.getThuTu());
			pre.setBoolean(3, entity.isNoiBat());
			pre.setBoolean(4, entity.isHienThi());
			pre.setInt(5, entity.getSoLanTaiVe());
			pre.setInt(6, entity.getGioiHanTaiVe());
			
			int effected = pre.executeUpdate();
			if(effected > 0) result = true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		freeConnection(connection);
		return result;
	}

	@Override
	public boolean delete(MaTaiVe entity) {
		Connection connection = getConnection();
		boolean result = false;
		String sql = "delete from mataive where mataive = ?";
		try {
			PreparedStatement pre = connection.prepareStatement(sql);
			pre.setString(1, entity.getMaTaiVe());
			
			int effected = pre.executeUpdate();
			if(effected > 0) result = true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		freeConnection(connection);
		return result;
	}

	@Override
	public boolean remove(MaTaiVe entity) {
		Connection connection = getConnection();
		boolean result = false;
		String sql = "update mataive set hienthi = ? where mataive = ?";
		try {
			PreparedStatement pre = connection.prepareStatement(sql);
			pre.setBoolean(1, false);
			pre.setString(2, entity.getMaTaiVe());
			
			int effected = pre.executeUpdate();
			if(effected > 0) result = true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		freeConnection(connection);
		return result;
	}

	@Override
	public boolean restore(MaTaiVe entity) {
		Connection connection = getConnection();
		boolean result = false;
		String sql = "update mataive set hienthi = ? where mataive = ?";
		try {
			PreparedStatement pre = connection.prepareStatement(sql);
			pre.setBoolean(1, true);
			pre.setString(2, entity.getMaTaiVe());
			
			int effected = pre.executeUpdate();
			if(effected > 0) result = true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		freeConnection(connection);
		return result;
	}

}
